package com.hongning.railway.ui.main.cadremanager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.FragmentCadreresultBinding;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Author: zhangjing
 * DATA:2019-08-29 19:25
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class CadreResultFragment extends BaseFragment<FragmentCadreresultBinding,BaseViewModel> {
    private boolean isSuccess=true;
    private int starttime = 5;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_cadreresult;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
//        isSuccess=getArguments().getBoolean(Constant.EXTRA_BOOLEAN);
    }

    @Override
    public void initData() {
        super.initData();
        if (isSuccess){
            binding.ivIcon.setImageResource(R.mipmap.icon_cadre_right_green);
            binding.tvResultTitle.setText("测评提交成功");
            binding.tvResultSubtitle.setText("测评提交成功，请点击按钮返回");
            initTimer();
        }else {
            /**
             * 感觉有问题 重新提交实在上一级页面提交的
             */
            binding.ivIcon.setImageResource(R.mipmap.icon_cadre_wrong_red);
            binding.tvResultTitle.setText("测评提交失败");
            binding.tvResultSubtitle.setText("测评提交失败，请点击按钮重新提交");
            binding.btnBack.setText("重试");
        }
        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("重试".equals(binding.btnBack.getText())){
                    ToastUtils.showShort("重试提交");
                }else {
                    getActivity().finish();
                }
            }
        });
    }

    void initTimer(){
        Observable.interval(1, TimeUnit.SECONDS)
                .compose(RxUtils.schedulersTransformer())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long along) throws Exception {
                        if (starttime-along>0){
                            String backstr=(starttime-along)+"S返回";
                            binding.btnBack.setText(backstr);
                        }else {
                            getActivity().finish();
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });
    }
}
